
import 'package:ditieapp/page/provider/LoginProvider.dart';
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';

import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../wiget/text_input.dart';
import '../yuekangma/ykm.dart';

class LoginPhoneScreen extends StatefulWidget {
  @override
  State<LoginPhoneScreen> createState() => _LoginPhoneScreenState();
}

class _LoginPhoneScreenState extends State<LoginPhoneScreen>
    with SingleTickerProviderStateMixin {
  final TextEditingController _phoneController = TextEditingController();

  final TextEditingController _nameController = TextEditingController();

  final TextEditingController _cityController = TextEditingController();

  late TextEditingController _controllerName = TextEditingController();

  late TextEditingController _controllerCity = TextEditingController();

  late TabController tabController;

  bool loadingVisible = false;

  late BuildContext dialogContext;

  void getShare(String name,String phone,String city)async{
    SharedPreferences prefs = await SharedPreferences.getInstance();

    await prefs.setString("name", name);
    await prefs.setString("phone", phone);
    await prefs.setString("city", city);
  }

  void getText(){
    //await SmartDialog.showToast('test toast');
  }

  void _requestData() {
    setState(() {
      loadingVisible = true;
    });

    showDialog(
        context: context,
        barrierColor: Colors.transparent, //背景透明
        barrierDismissible: true, // 屏蔽点击对话框外部自动关闭
        builder: (BuildContext context) {
          dialogContext = context;
          print(context.toString());
          return YmDialog("加载中…");
        });

    /// 延时2s执行返回
    Future.delayed(Duration(seconds: 2), (){
      Navigator.pop(dialogContext);
      Navigator.of(context).pushNamed("home");
    });

    print(context.size.toString());
  }


  @override
  void initState() {
    // TODO: implement initState
    super.initState();

  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView(
        children: [
          Container(
            alignment: Alignment.center,
            // color: Colors.green,
            child: Column(
              children: [
                SizedBox(
                  height: 35,
                ),
                Text(
                  "修改信息",
                  style: TextStyle(
                      fontSize: 35,
                      color: Colors.green,
                      fontWeight: FontWeight.w900),
                ),
                Padding(
                  padding: EdgeInsets.only(top: 30),
                ),
                // Text(
                //   ""+context.read<LoginProvider>().getNames().toString()+"  "+context.read<LoginProvider>().getPhones().toString()+"  "+context.read<LoginProvider>().getCityName().toString(),
                //   style: TextStyle(fontSize: 16, fontWeight: FontWeight.w700),
                //
                // ),
                SizedBox(
                  height: 25,
                ),
                // TimerCountDownWidget(
                //   onTimerFinish: () {},
                // ),
                // Padding(
                //   padding: EdgeInsets.all(20),
                //   child: Container(
                //     width: 400,
                //     height: 40,
                //     decoration: BoxDecoration(
                //         color: Colors.orange,
                //         borderRadius: BorderRadius.circular(20)),
                //     child: BrnTabBar(
                //       hasDivider: false,
                //       controller: tabController,
                //       tabs: tabs,
                //       onTap: (state, index) {
                //         state.refreshBadgeState(index);
                //       },
                //     ),
                //   ),
                // ),
                Container(
                  width: MediaQuery.of(context).size.width,
                  margin: EdgeInsets.symmetric(horizontal: 20),
                  child: TextInputField(
                    controller: _nameController,
                    labelText: "姓    名",
                    icon: Icons.account_circle,
                  ),
                ),
                SizedBox(
                  height: 25,
                ),
                Container(
                  width: MediaQuery.of(context).size.width,
                  decoration: BoxDecoration(
                      border: Border.all(style: BorderStyle.none)),
                  margin: EdgeInsets.symmetric(horizontal: 20),
                  child: TextInputField(
                    controller: _phoneController,
                    labelText: "手机号",
                    icon: Icons.phone,
                    isObscure: false,
                  ),
                ),

                SizedBox(
                  height: 25,
                ),
                Container(
                  width: MediaQuery.of(context).size.width,
                  margin: EdgeInsets.symmetric(horizontal: 20),
                  child: TextInputField(
                    controller: _cityController,
                    labelText: "地    址",
                    icon: Icons.home,
                  ),
                ),
                SizedBox(
                  height: 25,
                ),
                Container(
                  width: MediaQuery.of(context).size.width - 40,
                  height: 50,
                  decoration: BoxDecoration(
                    color: Colors.redAccent[200],
                    borderRadius: BorderRadius.all(
                      Radius.circular(5),
                    ),
                  ),
                  child: InkWell(
                    onTap: (){
                      print("Login");
                      //设置状态
                      if(_nameController.text.toString() != "null"){
                        context.read<LoginProvider>().setNames(_nameController.text.toString());
                      }

                      if(_phoneController.text.toString() != "null"){
                        context.read<LoginProvider>().setPhones(_phoneController.text.toString());
                      }

                      if(_cityController.text.toString() != "null"){
                        context.read<LoginProvider>().setCityName(_cityController.text.toString());
                      }

                      print(context.read<LoginProvider>().getNames().toString());
                      print(context.read<LoginProvider>().getPhones().toString());
                      print(context.read<LoginProvider>().getCityName().toString());

                      getShare(_nameController.text.toString(),_phoneController.text.toString(),_cityController.text.toString());

                      _requestData();

                    },
                    child: Center(
                      child: Text(
                        "保存",
                        style: TextStyle(
                            fontSize: 20, fontWeight: FontWeight.w700),
                      ),
                    ),
                  ),
                ),
                SizedBox(
                  height: 15,
                ),
                Text("场所码 和 行程修改"),
                SizedBox(
                  height: 10,
                ),
                Container(
                  width: MediaQuery.of(context).size.width,
                  margin: EdgeInsets.symmetric(horizontal: 20),
                  child: TextInputField(
                    controller: _controllerName,
                    labelText: "姓   名",
                    icon: Icons.account_circle,
                  ),
                ),
                SizedBox(
                  height: 10,
                ),
                Container(
                  width: MediaQuery.of(context).size.width,
                  margin: EdgeInsets.symmetric(horizontal: 20),
                  child: TextInputField(
                    controller: _controllerCity,
                    labelText: "场   所",
                    icon: Icons.home,
                  ),
                ),
                Container(
                  width: MediaQuery.of(context).size.width - 40,
                  height: 50,
                  decoration: BoxDecoration(
                    color: Colors.blueAccent,
                    borderRadius: BorderRadius.all(
                      Radius.circular(5),
                    ),
                  ),
                  child: InkWell(
                    onTap: (){

                      print("确定");
                      if (_controllerName.text.toString() != "null") {
                        context.read<LoginProvider>().setAlertName(
                            _controllerName.text.toString());
                      }

                      if (_controllerCity.text.toString() != "null") {
                        context.read<LoginProvider>().setAliertAddress(
                            _controllerCity.text.toString());
                      }

                      Navigator.pop(context);

                    },
                    child: Center(
                      child: Text(
                        "保存",
                        style: TextStyle(
                            fontSize: 20, fontWeight: FontWeight.w700),
                      ),
                    ),
                  ),
                ),
                // Row(
                //   mainAxisAlignment: MainAxisAlignment.center,
                //   children: [
                //     Text(
                //       'Don\'t have an account? ',
                //       style: TextStyle(
                //         fontSize: 20,
                //       ),
                //     ),
                //     InkWell(
                //       onTap: () {
                //         print("Register");
                //         Navigator.of(context).pushNamed("register");
                //       },
                //       child: Text(
                //         'Register',
                //         style: TextStyle(fontSize: 20, color: Colors.redAccent),
                //       ),
                //     )
                //   ],
                // ),
              ],
            ),
          )
        ],
      ),
    );
  }
}


class LoadingDialog extends Dialog {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Material(
        color: Colors.black38,
        child: Center(
          child: SizedBox(
            width: 120.0,
            height: 120.0,
            child: Container(
              decoration: ShapeDecoration(
                  color: Colors.white,
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.all(Radius.circular(8.0)))),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  CircularProgressIndicator(),
                  Padding(padding: EdgeInsets.only(top: 10.0)),
                  Text(
                    "加载中...",
                    style: TextStyle(fontSize: 16.0, color: Colors.black),
                  )
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}
